package cn.edu.nyist.integration.controller;

import cn.edu.nyist.integration.common.ServerResponse;
import cn.edu.nyist.integration.entity.Category;
import cn.edu.nyist.integration.entity.ProductCredit;
import cn.edu.nyist.integration.service.ProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("/list")
    public String productList(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "5")int pageSize,
                              Integer categoryId, Model model){
        List<Category> categories = productService.listAllCategory();
        model.addAttribute("categoryList",categories);
        PageInfo pageInfo = productService.listProduct(pageNum, pageSize, categoryId);
        model.addAttribute("pageInfo",pageInfo);
        return "page/product-manager";
    }

    @RequestMapping("/del")
    public String delProduct(@RequestParam Long productId, RedirectAttributes attributes){
        ServerResponse response = productService.delProduct(productId);
        if (!response.isSuccess()){
            //删除失败，提示信息
            attributes.addFlashAttribute("errorMsg",response.getMsg());
        }
        return "redirect:/product/list";
    }

    @RequestMapping("/toAddPage")
    public String toAddPage(Model model){
        List<Category> categories = productService.listAllCategory();
        model.addAttribute("categoryList",categories);
        return "page/product-add";
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addProduct(@RequestParam String productName,
                             @RequestParam Integer productScore,
                             @RequestParam Integer categoryId,RedirectAttributes attributes){
        Category category = new Category();
        category.setCategoryId(categoryId);
        ProductCredit productCredit = new ProductCredit();
        productCredit.setProductName(productName);
        productCredit.setProductScore(productScore);
        productCredit.setCategory(category);
        ServerResponse response = productService.addProduct(productCredit);
        if (!response.isSuccess()){
            //如果添加失败
            attributes.addFlashAttribute("errorMsg",response.getMsg());
            return "redirect:/product/toAddPage";
        }
        return "redirect:/product/list";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateProductScore(@RequestParam Integer score,@RequestParam String productName){
        System.out.println(score);
        System.out.println(productName);
        ServerResponse response = productService.updateProductScore(score,productName);

        return response;
    }



}
